//
//  SEInspListDetailParView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/22.
//

import Foundation
import UIKit

// MARK: - SEInspListDetailParView
class SEInspListDetailParView: UIView {
    let titleBV = UIView.createBase()
    let nameL = UILabel()
  
    let basicBV = InspListDetailBasicView()
    let recordBV = UIView.createBase()
   
    var records: [InspResultParam] = []
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -
    
    func checkValues(withItem item: InspResultParam) {
        var index = -1
        for (i, listParam) in records.enumerated() {
            if listParam.recordId == item.recordId { index = i }
        }
        if index != -1 { records.remove(at: index) }
        records.insert(item, at: index)
        updateRecordView()
    }
    
    // MARK: -
    func updateUI(withModal modal: ParListModal?) {
        nameL.text = modal?.serviceCompanyName
        
        basicBV.updateUI(withModal: modal)
        
        let patRs = modal?.patrolRecord ?? []
        
        records.removeAll()

        for item in patRs {
            let pItem = InspResultParam(tmplId: item.tmplId, tmplName: item.tmplName, recordId: item.recordId,
                                        status: item.status, remark: item.remark, summary: item.summary)
            records.append(pItem)
        }
        
        updateRecordView()
    }
    
    @objc func recordTapped(_ sender: InspListDetailItem) {
        let record = records[sender.tag]
        let vc = SEInspListResultVC(withModal: record)
        vc.didSelectItemWith = { (modal) -> () in
            self.checkValues(withItem: modal)
        }
        getFirstViewController()?.navigationController?.pushViewController(vc, animated: true)
    }
    
    func updateRecordView() {
        recordBV.removeAllSubViews()
        
        let recordTV = TitleItemView(withTitle: "巡检记录")
        recordBV.addSubview(recordTV)
        recordTV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }

        var lastI: InspListRecordItem?
        for (index, record) in records.enumerated() {
            let itemV = InspListRecordItem()
            itemV.update(withParamModal: record)
            itemV.tag = index
            itemV.addTarget(self, action: #selector(recordTapped), for: .touchUpInside)
            recordBV.addSubview(itemV)
            itemV.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.top.equalTo(lastI == nil ? recordTV.snp.bottom : lastI!.snp.bottom)
                if index == records.count - 1 {
                    make.bottom.equalToSuperview().offset(-10)
                }
            }
            lastI = itemV
        }
    }
    
    func setupTitleView() {
        addSubview(titleBV)
        titleBV.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        nameL.font = .systemFont(ofSize: 17)
        titleBV.addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.top.equalTo(titleBV.snp.top).offset(18)
            make.centerX.equalToSuperview()
        }
        
        let desL = UILabel()
        desL.text = "巡检服务单"
        desL.textColor = .hex("#306EC8")
        desL.font = .systemFont(ofSize: 16)
        titleBV.addSubview(desL)
        desL.snp.makeConstraints { make in
            make.top.equalTo(nameL.snp.bottom).offset(8)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-16)
        }
    }
    
    func setupUI() {
        setupTitleView()
        
        // 基本信息
        addSubview(basicBV)
        basicBV.snp.makeConstraints { make in
            make.top.equalTo(titleBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        // 巡检记录
        addSubview(recordBV)
        recordBV.snp.makeConstraints { make in
            make.top.equalTo(basicBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom)
        }
    }
}
